<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsSalesProfit extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $price_decimal = Db::name('kuyou_sets')->where(['name'=>'price_decimal'])->value('value');
            $number_decimal = Db::name('kuyou_sets')->where(['name'=>'number_decimal'])->value('value');

            $order_type = $this->request->post('order_type');
            $order_type = $order_type==1?1:2;

            if($order_type==1){//按客户
                $where = [];
                $purchase_time = $this->request->post('purchase_time');
                if(!empty($purchase_time)){
                    $str = explode('~',$purchase_time);
                    if(isset($str[0]) && isset($str[1])){
                        $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                    }
                }
                $customers_id = $this->request->post('customers_id');
                if(!empty($customers_id)){
                    $where['customers_id'] = $customers_id;
                }
                $customer_type = $this->request->post('customer_type');
                if(!empty($customer_type)){
                    $cid = Db::name('kuyou_customers')->where(['customertype_id'=>$customer_type])->column('id');
                    $where['customers_id'] = ["in", $cid];
                }

                $list = Db::name('kuyou_goods_sales_info')->where($where)->group('customers_id')->field('customers_id')->select();

                $listss = [];
                foreach ($list as $list_k2=>$list_v2){
                    $customers = Db::name('kuyou_customers')->where(['id'=>$list_v2['customers_id']])->field('customer_code,customertype_id,customer_name')->find();
                    if($customers){
                        $c_type = Db::name('kuyou_customertype')->where(['id'=>$customers['customertype_id']])->value('type_name');
                    }

                    $info = Db::name('kuyou_goods_sales_profit')->where(['customers_id'=>$list_v2['customers_id']])->select();

                    if($info){
                        $checkdate = Db::name('kuyou_checkout')->order(['checkdate'=>'DESC'])->value('checktime');
                        $enable_time = Db::name('kuyou_sets')->where(['name'=>'enable_time'])->value('value');
                        $map = [];
                        if(!empty($checkdate)){
                            $map['purchase_time'] = ['>',$checkdate];
                        }
                        $price_decimal = Db::name('kuyou_sets')->where(['name'=>'price_decimal'])->value('value');
                        $valuation_set= Db::name('kuyou_sets')->where(['name'=>'valuation'])->value('value');//计算方法
                        $stores_set= Db::name('kuyou_sets')->where(['name'=>'stores'])->value('is_checked');//是否分仓
                        if($valuation_set!=2){
                            $valuation_set = 1;
                        }
                        if($stores_set!=1){
                            $stores_set = 0;
                        }
                        $price_taxes= Db::name('kuyou_sets')->where(['name'=>'price_taxes'])->value('is_checked');
                        $taxes_set= Db::name('kuyou_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find();

                        $sort = 'purchase_time';$order='ASC';
                        $lists = Db::name('kuyou_goods_sales_profit')
                            ->where($map)
                            ->order($sort, $order)
                            ->select();
                        $goods_info = [];
                        $stores_ids = Db::name('kuyou_stores')->where(['status'=>'1'])->column('id');

                        foreach ($lists as $list_k=>$list_v){
                            $cost_all = 0;$pay_money = 0;
                            $infos = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$list_v['purchase_code']])->field('pay_money,basic_nmber,goods_code,stores_id,purchase_time')->select();
                            foreach ($infos as $info_k=>$info_v){
                                $goods = Db::name('kuyou_goods')->where(['goods_code'=>$info_v['goods_code']])->find();
                                if(!isset($goods_info[$info_v['goods_code']])){
                                    $where = [];
                                    if($stores_set==1){
                                        $where['stores_id'] = ['in',$stores_ids];
                                    }else{
                                        $where['stores_id'] = $info_v['stores_id'];
                                    }
                                    $where['goods_code'] = $info_v['goods_code'];

                                    if(empty($checkdate)){
                                        $start_time = strtotime($enable_time);
                                        $end_time = strtotime(date('Y-m-d',$info_v['purchase_time']).' 23:59:59');
                                        $wheretime['start_time'] = $start_time;
                                        $wheretime['end_time'] = $end_time;
                                        if(!empty($wheretime)){
                                            $where['purchase_time'] = ['between',[$wheretime['start_time'],time()]];
                                        }
                                        if($valuation_set==1){
                                            $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)->group('purchase_time')
                                                ->field('purchase_time,sum(basic_nmber) as basic_nmber,sum(all_money) as all_money')
                                                ->order(['purchase_time'=>'ASC'])->select();
                                        }else{
                                            $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)
                                                ->field('purchase_time,basic_nmber,all_money')
                                                ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                                        }
                                    }else{
                                        $checkout_id = Db::name('kuyou_checkout')->order(['checkdate'=>'DESC'])->value('id');
                                        $purchase_more = Db::name('kuyou_checkout_info')->where([
                                            'checkout_id'=>$checkout_id,
                                            'goods_code'=>$info_v['goods_code'],
                                        ])->find();
                                        $purchase_infos = Db::name('kuyou_goods_purchase')->where(['id'=>$purchase_more['purchase_id']])->find();

                                        $start_time = $checkdate;
                                        $end_time = strtotime(date('Y-m-d',$info_v['purchase_time']).' 23:59:59');
                                        $wheretime['start_time'] = $start_time;
                                        $wheretime['end_time'] = $end_time;
                                        if(!empty($wheretime)){
                                            $where['purchase_time'] = ['between',[$wheretime['start_time'],time()]];
                                        }
                                        if($valuation_set==1){
                                            $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)->group('purchase_time')
                                                ->field('purchase_time,sum(basic_nmber) as basic_nmber,sum(all_money) as all_money')
                                                ->order(['purchase_time'=>'ASC'])->select();
                                            if(!empty($purchase_more)){
                                                foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                                    if($purchase_info_v['purchase_time']==$purchase_more['purchase_time']){
                                                        $purchase_info[$purchase_info_k]['basic_nmber'] = (float)$purchase_more['av_basic_nmber'];
                                                        $purchase_info[$purchase_info_k]['all_money'] = (float)$purchase_more['av_all_money'];
                                                    }
                                                }
                                            }
                                        }else{
                                            if(empty($purchase_more)){
                                                $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)
                                                    ->field('purchase_time,basic_nmber,all_money')
                                                    ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                                            }else{
                                                $where['purchase_code'] = ['>=',$purchase_infos['purchase_code']];
                                                $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)
                                                    ->field('id,purchase_time,basic_nmber,all_money')
                                                    ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                                                foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                                    if($purchase_info_v['id']==$purchase_more['purchase_id']){
                                                        $purchase_info[$purchase_info_k]['basic_nmber'] = (float)$purchase_more['basic_nmber'];
                                                        $purchase_info[$purchase_info_k]['all_money'] = (float)$purchase_more['all_money'];
                                                    }
                                                }
                                            }
                                        }
                                    }


                                    $where = [];
                                    $where['goods_code'] = $info_v['goods_code'];
                                    $where['stores_id'] = ['in',$stores_ids];
                                    if(!empty($wheretime)){
                                        $where['createtime'] = ['between',[$wheretime['start_time'],time()]];
                                    }
                                    //期初
                                    $period = Db::name('kuyou_goods_period')->where($where)->field('period as basic_nmber,price as all_money,createtime as purchase_time')->find();
                                    if(!empty($period)){
                                        $period['purchase_time'] = strtotime(date('Y-m-d',$period['purchase_time']));
                                        if($valuation_set==1){
                                            $status = true;
                                            foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                                if($purchase_info_v['purchase_time']==$period['purchase_time']){
                                                    $status = false;
                                                    $purchase_info[$purchase_info_k]['basic_nmber'] += $period['basic_nmber'];
                                                    $purchase_info[$purchase_info_k]['all_money'] += $period['all_money'];
                                                }
                                            }
                                            if($status){
                                                $purchase_info[] = $period;
                                                $purchase_info = (new Commons())->arraySequence($purchase_info,'purchase_time','SORT_ASC');
                                            }
                                        }else{
                                            $purchase_infos = [];
                                            $purchase_infos[] = $period;
                                            foreach ($purchase_info as $k=>$v){
                                                $purchase_infos[] = $v;
                                            }
                                            $purchase_info = $purchase_infos;
                                        }
                                    }

                                    if($goods){
                                        $where = [];
                                        $where['goods_id'] = $goods['id'];
                                        $where['stores_id'] = ['in',$stores_ids];
                                        if(!empty($wheretime)){
                                            $where['purchase_time'] = ['between',[$wheretime['start_time'],time()]];
                                        }
                                        //成本调整
                                        $adjustment_money = 0;
                                        $adjustment = Db::name('kuyou_adjustment_info')->where($where)->select();
                                        foreach ($adjustment as $adjustment_k=>$adjustment_v){
                                            $advance_rate = 1;//单位换算进率
                                            if($adjustment_v['goods_unit']!=$goods['unit_id']){
                                                $unit = Db::name('kuyou_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$adjustment_v['goods_unit']])->value('advance_rate');
                                                if(!empty($unit) && $unit>0){
                                                    $advance_rate = $unit;
                                                }
                                            }
                                            $money = $adjustment_v['money']/$advance_rate;//换算后的调整成本
                                            $adjustment[$adjustment_k]['purchase_time'] = strtotime(date('Y-m-d',$adjustment_v['purchase_time']));
                                            $adjustment[$adjustment_k]['all_money'] = $adjustment_v['money'];
                                        }
                                        foreach ($adjustment as $adjustment_k=>$adjustment_v){
                                            foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                                if($purchase_info_v['purchase_time']==$adjustment_v['purchase_time']){
                                                    $purchase_info[$purchase_info_k]['all_money'] += $adjustment_v['all_money'];
                                                }
                                            }
                                        }
                                        //成本调整结束
                                    }
                                }else{
                                    $purchase_info = $goods_info[$info_v['goods_code']];
                                }

                                $cost = (new Commons())->getCost_Price($goods,'',$info_v['basic_nmber'],$purchase_info,$price_taxes,$valuation_set,$taxes_set);

                                if(isset($cost['purchase_info'])){
                                    $goods_info[$info_v['goods_code']] = $cost['purchase_info'];
                                    $purchase_info = $goods_info[$info_v['goods_code']];
                                    $cost = $cost['cost'];
                                }
                                $cost_all += $cost*$info_v['basic_nmber'];
                                $pay_money += $info_v['pay_money'];
                            }
                            $lists[$list_k]['pay_money'] = $pay_money;
                            if($list_v['purchase_type']==2){
                                $cost_all = -$cost_all;
                            }
                            $lists[$list_k]['sales_cost'] = round($cost_all,$price_decimal);
                            $lists[$list_k]['profit'] = round($list_v['all_money']-$cost_all,$price_decimal);
                            if($list_v['all_money']>0){
                                $lists[$list_k]['profit_rate'] = round($lists[$list_k]['profit']/$list_v['all_money']*100,2);
                            }else{
                                $lists[$list_k]['profit_rate'] = 0;
                            }
                            $lists[$list_k]['net_profit'] = round($lists[$list_k]['profit']-$list_v['preferential_money']-$list_v['purchase_money'],$price_decimal);
                            if($list_v['all_money']>0){
                                $lists[$list_k]['net_profit_rate'] = round($lists[$list_k]['net_profit']/$list_v['all_money']*100,2);
                            }else{
                                $lists[$list_k]['net_profit_rate'] = 0;
                            }
                        }

                        foreach ($info as $list_k=>$list_v){
                            $status = true;
                            foreach ($lists as $lists_k=>$lists_v){
                                if($list_v['id']==$lists_v['id']){
                                    $status = false;
                                    $info[$list_k]['pay_money'] = $lists[$lists_k]['pay_money'];
                                    $info[$list_k]['sales_cost'] = $lists[$lists_k]['sales_cost'];
                                    $info[$list_k]['profit'] = $lists[$lists_k]['profit'];
                                    $info[$list_k]['profit_rate'] = $lists[$lists_k]['profit_rate'];
                                    $info[$list_k]['net_profit'] = $lists[$lists_k]['net_profit'];
                                    $info[$list_k]['net_profit_rate'] = $lists[$lists_k]['net_profit_rate'];
                                }
                            }
                            if($status){
                                $cost_all = $list_v['sales_cost'];
                                $info[$list_k]['sales_cost'] = round($cost_all,$price_decimal);
                                $info[$list_k]['profit'] = round($list_v['all_money']-$cost_all,$price_decimal);
                                if($list_v['all_money']>0){
                                    $info[$list_k]['profit_rate'] = round($info[$list_k]['profit']/$list_v['all_money']*100,2);
                                }else{
                                    $info[$list_k]['profit_rate'] = 0;
                                }
                                $info[$list_k]['net_profit'] = round($info[$list_k]['profit']-$list_v['preferential_money']-$list_v['purchase_money'],$price_decimal);
                                if($list_v['all_money']>0){
                                    $info[$list_k]['net_profit_rate'] = round($info[$list_k]['net_profit']/$list_v['all_money']*100,2);
                                }else{
                                    $info[$list_k]['net_profit_rate'] = 0;
                                }
                            }
                        }
                    }

                    $count = 0;
                    $basic_nmber = 0;
                    $all_money = 0;
                    $tax_money = 0;
                    $money_tax_price = 0;
                    $cost_all = 0;
                    foreach ($info as $info_k=>$info_v){
                        if($info_v['purchase_type']==1){
                            $count += 1;
                            $basic_nmber += $info_v['number'];
                            $all_money += $info_v['all_money'];
                            $tax_money += $info_v['tax_money'];
                            $money_tax_price += $info_v['money_tax_price'];
                            $cost_all += $info_v['sales_cost'];
                        }else{
                            $basic_nmber -= $info_v['number'];
                            $all_money -= $info_v['all_money'];
                            $tax_money -= $info_v['tax_money'];
                            $cost_all -= $info_v['sales_cost'];
                        }
                    }
                    if($basic_nmber>0){
                        $unit_cost = round($cost_all/$basic_nmber,$price_decimal);
                    }else{
                        $unit_cost = 0;
                    }
                    $profit = $all_money-$cost_all;
                    if($profit>0){
                        $profit_rate = round($profit/$all_money*100,2);
                    }else{
                        $profit_rate = 0;
                    }
                    $listss[] = [
                        'c_type_name' => isset($c_type)?$c_type:'',
                        'customer_code' => isset($customers['customer_code'])?$customers['customer_code']:'',
                        'customer_name' => isset($customers['customer_name'])?$customers['customer_name']:'',
                        'count' => $count,
                        'basic_nmber' => round($basic_nmber,$number_decimal),
                        'all_money' => round($all_money,$price_decimal),
                        'tax_money' => round($tax_money,$price_decimal),
                        'money_tax_price' => round($money_tax_price,$price_decimal),
                        'unit_cost' => round($unit_cost,$price_decimal),
                        'cost_all' => round($cost_all,$price_decimal),
                        'profit' => round($profit,$price_decimal),
                        'profit_rate' => round($profit_rate,4),
                    ];
                }
                $lists = $listss;
            }else{//按商品
                $number_decimal = Db::name('kuyou_sets')->where(['name'=>'number_decimal'])->value('value');

                $where = [];
                $purchase_time = $this->request->post('purchase_time');
                if(!empty($purchase_time)){
                    $str = explode('~',$purchase_time);
                    if(isset($str[0]) && isset($str[1])){
                        $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                    }
                }
                $goods_type = $this->request->post('goods_type');
                if(!empty($goods_type)){
                    $cid = Db::name('kuyou_goods')->where(['goods_type_id'=>$goods_type])->column('goods_code');
                    $where['goods_code'] = ["in", $cid];
                }
                $goods_code = $this->request->post('goods_code');
                if(!empty($goods_code)){
                    $where['goods_code'] = ['like','%'.$goods_code.'%'];
                }

                $sort = 'purchase_time';$order='ASC';
                $checkdate = Db::name('kuyou_checkout')->order(['checkdate'=>'DESC'])->value('checktime');
                $map = [];
                if(!empty($checkdate)){
                    $map['purchase_time'] = ['>',$checkdate];
                }

                $lists = Db::name('kuyou_goods_sales_info')
                    ->where($map)
                    ->order($sort, $order)
                    ->select();

                $list = Db::name('kuyou_goods_sales_info')
                    ->where($where)
                    ->order($sort, $order)
                    ->select();

                if(!empty($list)){
                    $all_money_all = 0;$profit_all = 0;$net_profit_add = 0;

                    $checkdate = Db::name('kuyou_checkout')->order(['checkdate'=>'DESC'])->value('checktime');
                    $enable_time = Db::name('kuyou_sets')->where(['name'=>'enable_time'])->value('value');
                    $map = [];
                    if(!empty($checkdate)){
                        $map['purchase_time'] = ['>',$checkdate];
                    }
                    $price_decimal = Db::name('kuyou_sets')->where(['name'=>'price_decimal'])->value('value');
                    $valuation_set= Db::name('kuyou_sets')->where(['name'=>'valuation'])->value('value');//计算方法
                    $stores_set= Db::name('kuyou_sets')->where(['name'=>'stores'])->value('is_checked');//是否分仓
                    if($valuation_set!=2){
                        $valuation_set = 1;
                    }
                    if($stores_set!=1){
                        $stores_set = 0;
                    }
                    $price_taxes= Db::name('kuyou_sets')->where(['name'=>'price_taxes'])->value('is_checked');
                    $taxes_set= Db::name('kuyou_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find();
                    $stores_ids = Db::name('kuyou_stores')->where(['status'=>'1'])->column('id');

                    $goods_info = [];
                    foreach ($lists as $list_k=>$list_v){
                        $cost_all = 0;$pay_money = 0;
                        if($list_v['purchase_type']==1){
                            $info = Db::name('kuyou_goods_sales_info')->where(['purchase_code'=>$list_v['purchase_code'],'goods_code'=>$list_v['goods_code']])->field('pay_money,basic_nmber,goods_code,stores_id,purchase_time')->select();
                            $info_goods = [];
                            foreach ($info as $info_k=>$info_v){
                                $goods = Db::name('kuyou_goods')->where(['goods_code'=>$info_v['goods_code']])->find();
                                if(!isset($goods_info[$info_v['goods_code']])){
                                    $where = [];
                                    if($stores_set==1){
                                        $where['stores_id'] = ['in',$stores_ids];
                                    }else{
                                        $where['stores_id'] = $info_v['stores_id'];
                                    }
                                    $where['goods_code'] = $info_v['goods_code'];

                                    if(empty($checkdate)){
                                        $start_time = strtotime($enable_time);
                                        $end_time = strtotime(date('Y-m-d',$info_v['purchase_time']).' 23:59:59');
                                        $wheretime['start_time'] = $start_time;
                                        $wheretime['end_time'] = $end_time;
                                        if(!empty($wheretime)){
                                            $where['purchase_time'] = ['between',[$wheretime['start_time'],time()]];
                                        }
                                        if($valuation_set==1){
                                            $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)->group('purchase_time')
                                                ->field('purchase_time,sum(basic_nmber) as basic_nmber,sum(all_money) as all_money')
                                                ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                                        }else{
                                            $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)
                                                ->field('purchase_time,basic_nmber,all_money')
                                                ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                                        }
                                    }else{
                                        $checkout_id = Db::name('kuyou_checkout')->order(['checkdate'=>'DESC'])->value('id');
                                        $purchase_more = Db::name('kuyou_checkout_info')->where([
                                            'checkout_id'=>$checkout_id,
                                            'goods_code'=>$info_v['goods_code'],
                                        ])->find();
                                        $purchase_infos = Db::name('kuyou_goods_purchase')->where(['id'=>$purchase_more['purchase_id']])->find();

                                        $start_time = $checkdate;
                                        $end_time = strtotime(date('Y-m-d',$info_v['purchase_time']).' 23:59:59');
                                        $wheretime['start_time'] = $start_time;
                                        $wheretime['end_time'] = $end_time;
                                        if(!empty($wheretime)){
                                            $where['purchase_time'] = ['between',[$wheretime['start_time'],time()]];
                                        }
                                        if($valuation_set==1){
                                            $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)->group('purchase_time')
                                                ->field('purchase_time,sum(basic_nmber) as basic_nmber,sum(all_money) as all_money')
                                                ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                                            if(!empty($purchase_more)){
                                                foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                                    if($purchase_info_v['purchase_time']==$purchase_more['purchase_time']){
                                                        $purchase_info[$purchase_info_k]['basic_nmber'] = (float)$purchase_more['av_basic_nmber'];
                                                        $purchase_info[$purchase_info_k]['all_money'] = (float)$purchase_more['av_all_money'];
                                                    }
                                                }
                                            }
                                        }else{
                                            if(empty($purchase_more)){
                                                $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)
                                                    ->field('purchase_time,basic_nmber,all_money')
                                                    ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                                            }else{
                                                $where['purchase_code'] = ['>=',$purchase_infos['purchase_code']];
                                                $purchase_info = Db::name('kuyou_goods_purchase_info')->where($where)
                                                    ->field('id,purchase_time,basic_nmber,all_money')
                                                    ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                                                foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                                    if($purchase_info_v['id']==$purchase_more['purchase_id']){
                                                        $purchase_info[$purchase_info_k]['basic_nmber'] = (float)$purchase_more['basic_nmber'];
                                                        $purchase_info[$purchase_info_k]['all_money'] = (float)$purchase_more['all_money'];
                                                    }
                                                }
                                            }
                                        }
                                    }


                                    $where = [];
                                    $where['goods_code'] = $info_v['goods_code'];
                                    $where['stores_id'] = ['in',$stores_ids];
                                    if(!empty($wheretime)){
                                        $where['createtime'] = ['between',[$wheretime['start_time'],time()]];
                                    }
                                    //期初
                                    $period = Db::name('kuyou_goods_period')->where($where)->field('period as basic_nmber,price as all_money,createtime as purchase_time')->find();
                                    if(!empty($period)){
                                        $period['purchase_time'] = strtotime(date('Y-m-d',$period['purchase_time']));
                                        if($valuation_set==1){
                                            $status = true;
                                            foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                                if($purchase_info_v['purchase_time']==$period['purchase_time']){
                                                    $status = false;
                                                    $purchase_info[$purchase_info_k]['basic_nmber'] += $period['basic_nmber'];
                                                    $purchase_info[$purchase_info_k]['all_money'] += $period['all_money'];
                                                }
                                            }
                                            if($status){
                                                $purchase_info[] = $period;
                                                $purchase_info = (new Commons())->arraySequence($purchase_info,'purchase_time','SORT_ASC');
                                            }
                                        }else{
                                            $purchase_infos = [];
                                            $purchase_infos[] = $period;
                                            foreach ($purchase_info as $k=>$v){
                                                $purchase_infos[] = $v;
                                            }
                                            $purchase_info = $purchase_infos;
                                        }
                                    }

                                    if($goods){
                                        $where = [];
                                        $where['goods_id'] = $goods['id'];
                                        $where['stores_id'] = ['in',$stores_ids];
                                        if(!empty($wheretime)){
                                            $where['purchase_time'] = ['between',[$wheretime['start_time'],time()]];
                                        }
                                        //成本调整
                                        $adjustment_money = 0;
                                        $adjustment = Db::name('kuyou_adjustment_info')->where($where)->select();
                                        foreach ($adjustment as $adjustment_k=>$adjustment_v){
                                            $advance_rate = 1;//单位换算进率
                                            if($adjustment_v['goods_unit']!=$goods['unit_id']){
                                                $unit = Db::name('kuyou_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$adjustment_v['goods_unit']])->value('advance_rate');
                                                if(!empty($unit) && $unit>0){
                                                    $advance_rate = $unit;
                                                }
                                            }
                                            $money = $adjustment_v['money']/$advance_rate;//换算后的调整成本
                                            $adjustment[$adjustment_k]['purchase_time'] = strtotime(date('Y-m-d',$adjustment_v['purchase_time']));
                                            $adjustment[$adjustment_k]['all_money'] = $adjustment_v['money'];
                                        }
                                        foreach ($adjustment as $adjustment_k=>$adjustment_v){
                                            foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                                if($purchase_info_v['purchase_time']==$adjustment_v['purchase_time']){
                                                    $purchase_info[$purchase_info_k]['all_money'] += $adjustment_v['all_money'];
                                                }
                                            }
                                        }
                                        //成本调整结束
                                    }
                                }else{
                                    $purchase_info = $goods_info[$info_v['goods_code']];
                                }

                                $cost = (new Commons())->getCost_Price($goods,'',$info_v['basic_nmber'],$purchase_info,$price_taxes,$valuation_set,$taxes_set);

                                if(isset($cost['purchase_info'])){
                                    $goods_info[$info_v['goods_code']] = $cost['purchase_info'];
                                    $purchase_info = $goods_info[$info_v['goods_code']];
                                    $cost = $cost['cost'];
                                }
                                $cost_all += $cost*$info_v['basic_nmber'];
                                $pay_money += $info_v['pay_money'];
                                $info_goods[$info_v['goods_code']] = $cost;
                            }
                        }
                        $lists[$list_k]['info_goods'] = $info_goods;
                        $lists[$list_k]['pay_money'] = $pay_money;
                        $lists[$list_k]['sales_cost'] = round($cost_all,$price_decimal);
                        $lists[$list_k]['profit'] = round($list_v['all_money']-$cost_all,$price_decimal);
                        if($list_v['all_money']>0){
                            $lists[$list_k]['profit_rate'] = round($lists[$list_k]['profit']/$list_v['all_money']*100,2);
                        }else{
                            $lists[$list_k]['profit_rate'] = 0;
                        }
                        $lists[$list_k]['net_profit'] = round($lists[$list_k]['profit']-$list_v['preferential_money']-$list_v['purchase_money'],$price_decimal);
                        if($list_v['all_money']>0){
                            $lists[$list_k]['net_profit_rate'] = round($lists[$list_k]['net_profit']/$list_v['all_money']*100,2);
                        }else{
                            $lists[$list_k]['net_profit_rate'] = 0;
                        }
                    }

                    foreach ($list as $list_k=>$list_v){
                        $status = true;
                        foreach ($lists as $lists_k=>$lists_v){
                            if($list_v['id']==$lists_v['id']){
                                $status = false;
                                $list[$list_k]['pay_money'] = $lists[$lists_k]['pay_money'];
                                $list[$list_k]['sales_cost'] = $lists[$lists_k]['sales_cost'];
                                $list[$list_k]['profit'] = $lists[$lists_k]['profit'];
                                $list[$list_k]['profit_rate'] = $lists[$lists_k]['profit_rate'];
                                $list[$list_k]['net_profit'] = $lists[$lists_k]['net_profit'];
                                $list[$list_k]['net_profit_rate'] = $lists[$lists_k]['net_profit_rate'];
                                $list[$list_k]['info_goods'] = $lists_v['info_goods'];
                                $all_money_all += $lists[$lists_k]['all_money'];
                                $profit_all += $lists[$lists_k]['profit'];
                                $net_profit_add += $lists[$lists_k]['net_profit'];
                            }
                        }
                        if($status){
                            $cost_all = (float)(Db::name('kuyou_goods_sales_profit')
                                ->where(['purchase_code'=>$list_v['purchase_code']])->value('sales_cost'));
                            $list[$list_k]['sales_cost'] = round($cost_all,$price_decimal);
                            $list[$list_k]['profit'] = round($list_v['all_money']-$cost_all,$price_decimal);
                            if($list_v['all_money']>0){
                                $list[$list_k]['profit_rate'] = round($list[$list_k]['profit']/$list_v['all_money']*100,2);
                            }else{
                                $list[$list_k]['profit_rate'] = 0;
                            }
                            $list[$list_k]['net_profit'] = round($list[$list_k]['profit']-$list_v['preferential_money']-$list_v['purchase_money'],$price_decimal);
                            if($list_v['all_money']>0){
                                $list[$list_k]['net_profit_rate'] = round($list[$list_k]['net_profit']/$list_v['all_money']*100,2);
                            }else{
                                $list[$list_k]['net_profit_rate'] = 0;
                            }
                            $list[$list_k]['info_goods'] = [];
                        }
                    }
                    $profit_list = [];$cost_price_list = [];
                    foreach ($list as $list_k=>$list_v){
                        $profit_list[$list_v['id']] = $list_v['sales_cost'];
                        $cost_price_list[$list_v['id']] = $list_v['info_goods'];
                    }
                    unset($list);


                    $where = [];
                    $purchase_time = $this->request->post('purchase_time');
                    if(!empty($purchase_time)){
                        $str = explode('~',$purchase_time);
                        if(isset($str[0]) && isset($str[1])){
                            $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                        }
                    }
                    $goods_type = $this->request->post('goods_type');
                    if(!empty($goods_type)){
                        $cid = Db::name('kuyou_goods')->where(['goods_type_id'=>$goods_type])->column('goods_code');
                        $where['goods_code'] = ["in", $cid];
                    }
                    $goods_code = $this->request->post('goods_code');
                    if(!empty($goods_code)){
                        $where['goods_code'] = ['like','%'.$goods_code.'%'];
                    }
                    $lists=[];
                    $list = Db::name('kuyou_goods_sales_info')->where($where)->group('goods_code')->field('goods_code')->select();
                    foreach ($list as $list_k=>$list_v){
                        $goods = Db::name('kuyou_goods')->where(['goods_code'=>$list_v['goods_code']])->field('goods_code,goods_type_id,goods_name')->find();
                        if($goods){
                            $c_type = Db::name('kuyou_goods_type')->where(['id'=>$goods['goods_type_id']])->value('supplier_type_name');
                        }

                        $info = Db::name('kuyou_goods_sales_info')->where(['goods_code'=>$list_v['goods_code']])->select();
                        $count = 0;
                        $basic_nmber = 0;
                        $all_money = 0;
                        $tax_money = 0;
                        $money_tax_price = 0;
                        $cost_all = 0;
                        foreach ($info as $info_k=>$info_v){
                            if($info_v['purchase_type']==1){
                                $count += 1;
                                $basic_nmber += $info_v['basic_nmber'];
                                $all_money += $info_v['all_money'];
                                $tax_money += $info_v['tax_money'];
                                $money_tax_price += $info_v['money_tax_price'];
                                $cost_all += isset($profit_list[$info_v['id']])?$profit_list[$info_v['id']]:0;
                            }else{
                                $basic_nmber -= $info_v['basic_nmber'];
                                $all_money -= $info_v['all_money'];
                                $tax_money -= $info_v['tax_money'];
                                $money_tax_price -= $info_v['money_tax_price'];
                                $cost_price = isset($cost_price_list[$info_v['id']])?$cost_price_list[$info_v['id']]:[];
                                $cost_price = isset($cost_price[$info_v['goods_code']])?$cost_price[$info_v['goods_code']]:'';
                                if($cost_price===''){
                                    $cost_price = Db::name('kuyou_goods')->where(['goods_code'=>$info_v['goods_code']])->value('purchase');
                                }
                                $cost_all -= $cost_price*$info_v['basic_nmber'];
                            }
                        }
                        if($basic_nmber>0){
                            $unit_cost = round($cost_all/$basic_nmber,$price_decimal);
                        }else{
                            $unit_cost = 0;
                        }
                        $profit = $all_money-$cost_all;
                        if($profit>0){
                            $profit_rate = round($profit/$all_money*100,2);
                        }else{
                            $profit_rate = 0;
                        }
                        $lists[] = [
                            'c_type_name' => isset($c_type)?$c_type:'',
                            'customer_code' => isset($goods['goods_code'])?$goods['goods_code']:'',
                            'customer_name' => isset($goods['goods_name'])?$goods['goods_name']:'',
                            'count' => $count,
                            'basic_nmber' => round($basic_nmber,$number_decimal),
                            'all_money' => round($all_money,$price_decimal),
                            'tax_money' => round($tax_money,$price_decimal),
                            'money_tax_price' => round($money_tax_price,$price_decimal),
                            'unit_cost' => round($unit_cost,$price_decimal),
                            'cost_all' => round($cost_all,$price_decimal),
                            'profit' => round($profit,$price_decimal),
                            'profit_rate' => round($profit_rate,$price_decimal),
                        ];
                    }
                }else{
                    $lists=[];
                }
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            $count = sizeof($lists);
            if($pageStatus){
                $min = ($page-1)*$limit;
                $max = $page*$limit;
                foreach ($lists as $lists_k=>$lists_v){
                    if($lists_k<$min || $lists_k>=$max){
                        unset($lists[$lists_k]);
                    }
                }
            }

            $result = array("count" => $count, "list" => $lists);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
